# -*- codeing = utf-8 -*-
# @Time : 2021/2/28 10:41
# @Atuthor: 朱朱
# @File: 153装饰器实现权限管理.py
# @Software: PyCharm
""
"""
1.给定基础的权限因子
2.给定用户权限
3.采用@装饰器(权限),来装饰函数
4.采用位运算 &
"""
user_permission = 11

# 权限因子
# 用户权限  &  权限因子 != 0
DEL_PERMISSION = 8  # 1011 & 1000  ==>  1000
READ_PERMISSION = 4  # 1011 & 0100 ==> 0000
WRITE_PERMISSION = 2   # 1011 & 0010 ==> 0010
EXE_PERMISSION = 1   # 1011 & 0001 ==> 0001

def check_permission(x,y):
    def handle_action(fn):
        def do_action():
            if x &y !=0:
                fn()
            else:print('对不起,你没有权限,滚蛋')
        return do_action
    return handle_action

@check_permission(user_permission,READ_PERMISSION)
def read():
    print('我正在读取内容...')

@check_permission(user_permission,WRITE_PERMISSION)
def write():
    print('我正在写内容...')

@check_permission(user_permission,EXE_PERMISSION)
def execute():
    print('我正在执行内容..')

@check_permission(user_permission,DEL_PERMISSION)
def delete():
    print('我正在删除内容...')

read()
write()
execute()
delete()